<?php

class Ccategoria extends CI_Model {


    
    
    
function get_all_categoria_com_paginacao($inicial,$numreg){
        
        if(!is_numeric($inicial)){
            $inicial=0;
        }
        if(!is_numeric($numreg)){
            $numreg=0;
        }
        
        $query = $this->db->query('SELECT * FROM mov_categorias where cate_principal = "n" and cate_id_fk = "0" order by cate_id desc LIMIT '.$inicial.', '.$numreg);
        return $query->result();
        
    } 
    
function conta_registro_categoria(){

        $query = $this->db->get_where('mov_categorias', array('cate_id_fk' => '0','cate_principal' => 'n','cate_id_fk' => '0'));
        return $query->num_rows();
    }

function get_all_sub_categoria_com_paginacao($id_cate_anterior,$inicial,$numreg){
        
        if(!is_numeric($inicial)){
            $inicial=0;
        }
        if(!is_numeric($numreg)){
            $numreg=0;
        }
        
        $query = $this->db->query('SELECT * FROM mov_categorias where cate_principal = "n" and cate_id_fk <> "0" and cate_id_fk = '.$id_cate_anterior.' order by cate_id desc LIMIT '.$inicial.', '.$numreg);
        return $query->result();
        
    } 
    
    
function conta_registro_sub_categoria($id_cate_anterior){
        
        $query = $this->db->query('select * from mov_categorias where cate_principal = "n" and cate_id_fk <> "0" and cate_id_fk = '.$id_cate_anterior.'');
        return $query->num_rows();
}
        
    
function get_all_categoria_do_menu($inicial,$numreg){
        
        if(!is_numeric($inicial)){
            $inicial=0;
        }
        if(!is_numeric($numreg)){
            $numreg=0;
        }
        
        $query = $this->db->query('SELECT * FROM mov_categorias where cate_id_fk = 0 and cate_principal = "s" order by cate_id desc LIMIT '.$inicial.', '.$numreg);
        return $query->result();
        
} 

function get_all_menu_principal(){
                
        $query = $this->db->query('SELECT * FROM mov_categorias where cate_id_fk = 0 and cate_principal = "s" and cate_posicao != "n" order by cate_posicao');
        return $query->result();    
} 

function get_all_categorias(){
                
        $query = $this->db->query('SELECT * FROM mov_categorias where cate_id_fk = 0 and cate_principal = "n" order by cate_posicao');
        return $query->result();    
} 
    
function conta_registro_categoria_do_menu(){
        
        $query = $this->db->get_where('mov_categorias', array('cate_id_fk' => '0','cate_principal' => 's'));
        return $query->num_rows();
    }
        
    
public function adicionar_nova_categoria($dados){


        if($this->db->insert('mov_categorias', $dados)){
            return true;
        }else{
            return false;
        }
}    
    
public function solicita_dados_cate($id){

       
        $query = $this->db->query('SELECT * FROM mov_categorias where cate_id ='.$id);
        return $query->result(); 
    

}      
    
function altera_uma_categoria($id,$cate){
          
        $this->db->update('mov_categorias',$cate,array('cate_id' => $id));
        return 1;   
}
    
       
public function exclui_uma_categoria($id){
                
        if($this->db->query("DELETE FROM mov_categorias WHERE cate_id =".$id." or cate_id_fk =".$id)){
		
                    return 1;
		}else{
                    
                    return 2;
		}
}    
    
function solicita_nome_opcoes_menu(){
                
        $query = $this->db->query('SELECT cate_nome as nome, cate_id as id FROM mov_categorias where cate_id_fk = 0 
        and cate_principal = "s" and cate_posicao <> "n" order by cate_id desc');
        return $query->result();      
}     
    
function solicita_nome_de_uma_opcoes_menu($id){
    
       if(!is_numeric($id)){
            $id=99999;
        }
        $query = $this->db->query('SELECT cate_nome as nome FROM mov_categorias where cate_id_fk = 0 
        and cate_principal = "s" and cate_posicao <> "n" and cate_id = '.$id.' order by cate_id desc');
        return $query->result();    
}  

function solicita_nome_de_uma_categoria($id){
    
       if(!is_numeric($id)){
            $id=99999;
        }
      
        $query = $this->db->query('SELECT cate_nome as nome FROM mov_categorias where cate_id = '.$id.' order by cate_id desc');
        return $query->result();     
}    
    
function retorna_nome_do_banner($id){
              
        $this->db->select('cate_img');
        $query = $this->db->get_where('mov_categorias', array('cate_id' => $id));
        return $query->result();

    }    
    
function excluir_banners($id){

        $this->db->where('cate_id',$id);
        if($this->db->update('mov_categorias', array('cate_img' => 'n'))){
            return true;
        }
        else{
            return false;
        }   
}

function retorna_se_categoria_eh_zero_ou_nao($id){
              
        $this->db->select('cate_id_fk');
        $query = $this->db->get_where('mov_categorias', array('cate_id' => $id));
        return $query->result();
}  
    
public function verifica_se_menu_foi_relacionado($id){
    
    $query = $this->db->query('SELECT * FROM mov_categorias where cate_principal = "n" and cate_relacao_menu ='.$id);
    return $query->num_rows();   
}
    
public function verifica_se_a_categoria_esta_relacionada_com_um_produto($id){
    
    $query = $this->db->query('SELECT * FROM mov_produtos where prod_categoria ='.$id);
    return $query->num_rows();   
}    

public function verifica_se_a_subcategoria_esta_relacionada_com_um_produto($id){

    $query = $this->db->query('SELECT * FROM mov_produtos where prod_sub_categoria ='.$id);
    return $query->num_rows();   
}  

function solicita_nome_de_uma_categoria_string($id){
    
       if(!is_numeric($id)){
            $id=99999;
        }
        $query = $this->db->query('SELECT cate_nome as nome FROM mov_categorias where cate_id = '.$id);
        $nome = $query->result();
        $query = $nome[0]->nome;
        return $query; 
}    

public function verifica_se_a_subcategoria_tem_subcategorias($id){

    $query = $this->db->query('SELECT * FROM mov_categorias where cate_id_fk ='.$id);
    return $query->num_rows();   
}     
    
    


}